package com.lanou3g.musicenjoy.activities;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.widget.MediaController;
import android.widget.VideoView;

import com.alibaba.fastjson.JSON;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou3g.musicenjoy.R;
import com.lanou3g.musicenjoy.entity.New;
import com.lanou3g.musicenjoy.entity.NewOne;
import com.lanou3g.musicenjoy.volley.VolleySingleton;

import org.json.JSONObject;

import java.io.File;
import java.net.URL;

/**
 * Created by dllo on 15/10/22.
 */
public class NewActivity extends Activity {
    private NewOne.ResultEntity.VideoInfoEntity entity;
    private RequestQueue queue;
    private VideoView videoView;
    MediaController mediaController;
    String url;
    String b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new);
        queue= VolleySingleton.getInstance(this).getRequestQueue();
        Intent intent=getIntent();
        String a=intent.getStringExtra("a");
        Log.i("dhfksf",a);
        String urll="http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0&method=baidu.ting.mv.playMV&format=json&mv_id="+a+"&song_id=&definition=0";
        Log.i("fhakg", urll);
        final StringRequest request=new StringRequest(urll, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (response!=null){
                    NewOne result= JSON.parseObject(response, NewOne.class);
                    Log.i("xinxin", result + "111111");
                    Log.i("FDJH",result+"");
                    if (result!=null){

                       entity= result.getResult().getVideo_info();
                       url=entity.getSourcepath();
                        Log.i("djffafh", url);
                       b=  url.substring(31);
                        Log.i("FHSF",url.substring(31));
                        playMedia();
                    }

                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        queue.add(request);

    }

    private void playMedia() {
        //让屏幕横向显示
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        videoView = (VideoView) findViewById(R.id.newVideoView);
        mediaController = new MediaController(this);
        String urlll = "http://www.yinyuetai.com/mv/video-url/"+b;
        Log.i("DHH",urlll);
        Uri uri = Uri.parse(urlll);
        videoView.setVideoURI(uri);
        videoView.setMediaController(mediaController);
        videoView.start();
        videoView.requestFocus();
    }
}
